<?php

/**
 * This class handles the DOMDocument generation plus addons that are used
 * internally by Template Engine compiler
 *
 * @author Guilherme Blanco
 * @copyright Bisna
 */

class TemplateDOMDocument extends DOMDocument
{
	private static $instance = FALSE;


	/**
     * This is meant to be called in a static context.
     * TemplateDOMDocument::getDocument()
     *
     * @return TemplateDOMDocument
     * @access public
     * @static
     */
	public static function getDocument()
	{
		if ( ! self::$instance ) {
			$c = __CLASS__;
			self::$instance = new $c();
		}

		return self::$instance;
	}


	public function getElementById( $elementId )
	{
    	$DOMElement = parent::getElementById( $elementId );

    	if ( $DOMElement === NULL ) {
        	// Slow way to retrieve DOMElement
			$XPath = new DOMXPath( $this );
			$rs = $XPath->query( "//*[@id = '" . $elementId . "']" );

			$DOMElement = $rs->item( 0 ); // Return NULL if not found
		}

		return $DOMElement;
	}
}

?>